class ObservablePromise {
    constructor() {
        this.observers = new Map();
        this.promise = null;
    }

    // 创建Promise
    createPromise() {
        this.promise = new Promise((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
        });
        return this.promise;
    }

    // 订阅事件
    subscribe(event, callback) {
        if (!this.observers.has(event)) {
            this.observers.set(event, []);
        }
        this.observers.get(event).push(callback);
    }

    // 发布事件
    publish(event, data) {
        const callbacks = this.observers.get(event) || [];
        callbacks.forEach(callback => callback(data));
    }

    // 触发成功
    resolve(data) {
        this.publish('success', data);
        this.resolve(data);
    }

    // 触发失败
    reject(error) {
        this.publish('error', error);
        this.reject(error);
    }
}

// 使用示例
const observablePromise = new ObservablePromise();
const promise = observablePromise.createPromise();

observablePromise.subscribe('success', (data) => {
    console.log('Success event:', data);
});

observablePromise.subscribe('error', (error) => {
    console.log('Error event:', error);
});

promise.then(data => {
    console.log('Promise resolved:', data);
}).catch(error => {
    console.log('Promise rejected:', error);
});

observablePromise.resolve('Hello World');